<?php
class Admin_page_Controller extends Template_Controller {

	public $search;
	public $template = 'admin/index';
	
    public function __construct()
    {
        parent::__construct();
        
        $this->search = array('keyword' => '','page' => 0);
        
        $this->_get_submit();
    }
    
	public function __call($method, $arguments)
	{
		// Disable auto-rendering
		$this->auto_render = FALSE;
	}
	
	function _get_submit()
	{
		if($this->session->get('error_msg'))
			$this->template->error_msg = $this->session->get('error_msg');
		if($this->session->get('warning_msg'))
			$this->template->warning_msg = $this->session->get('warning_msg');
		if($this->session->get('success_msg'))
			$this->template->success_msg = $this->session->get('success_msg');
		if($this->session->get('info_msg'))
			$this->template->info_msg = $this->session->get('info_msg');
		if($this->session->get('frm')){			
			$this->template->content->mr = $this->session->get('frm');
		}
	}
	
    public function index()
    {	
        $this->showlist();
    }
    
    public function showlist()
    {
    	$this->template->content = new View('admin_page/list');
    		
		//Assign
		$this->template->content->keyword = $this->search['keyword'];
		
		$this->where_sql();
		
		//Pagination
    	$this->pagination = new Pagination(array(
    		'base_url'    => 'admin_page/search/',
		    'uri_segment'    => 'page',
		    'total_items'    => ORM::factory('page')->count_all(),
		    'items_per_page' => $this->site['site_num_line2'],
		    'style'          => 'classic',
		));		
		$this->db->limit($this->pagination->items_per_page,$this->pagination->sql_offset);
    	
    	$model = new Page_Model();
    	$this->template->content->mlist = $model->get();
    }
    
    public function where_sql()
    {
    	if($this->search['keyword'])
    		$this->db->like('pages_firstname',$this->search['keyword']);
    }
    
    public function search()
    {    	
    	//Get    	
    	if($this->session->get('sess_search')){			
			$this->search = $this->session->get('sess_search');
		}
		if($this->input->post('txt_keyword')){    		
			$this->search['keyword'] = $this->input->post('txt_keyword');
		} else 
			$this->search['keyword'] = '';
		
		//Page
		$page = $this->uri->segment('page');
		if($page)
			$this->search['page'] = $page;
			
		//Set
		$this->session->set_flash('sess_search',$this->search);
		
		$this->showlist();
    }
    
    public function create()
    {
    	$this->template->content = new View('admin_page/frm');
    }
    
    public function edit($id)
    {	
    	$this->template->content = new View('admin_page/frm');
    	
    	$this->template->content->mr = ORM::factory('page')->find($id);
    }
    
    private function _get_record()
    {
    	$form = array
	    (
	        'txt_title' => '',
	        'txt_content' => '',
	        'txt_order' => '',
	        'sel_status' => '',
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_title','required');
			$post->add_rules('txt_content','required');
			if($post->validate())
 			{
 				$form = arr::overwrite($form, $post->as_array());
 				return $form; 				
			} else {
				$errors = arr::overwrite($errors, $post->errors('page_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				$hd_id = $this->input->post('hd_id');
				if($hd_id)
					url::redirect('admin_page/edit/'.$hd_id);
				else
					url::redirect('admin_page/create');
				die();
			}
        }
    }
    
    public function save()
    {
    	$hd_id = $this->input->post('hd_id');
    	
    	$record = $this->_get_record();
		
		if($record)
		{
			if(!$hd_id){
				$result = ORM::factory('page');
			} else {
				$result = ORM::factory('page',$hd_id);
			}	
			$result->page_title = $record['txt_title'];
			$result->page_content = $record['txt_content'];
			$result->page_order = $record['txt_order'];
			$result->page_active = $record['sel_status'];			
			
			$hd_id = $result->save();
			
			if(!$this->input->post('hd_id'))
				$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_add'));
			else
				$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_save'));
		}
		if($this->input->post('btn_save_add'))
			url::redirect('admin_page/create');
		else
			url::redirect('admin_page/edit/'.$hd_id);
		die();    	
    }
    
    public function saveall()
    {
    	$arr_id = $this->input->post('chk_id');
		
		if(is_array($arr_id)){
			$sel_action = $this->input->post('sel_action');
		
			if($sel_action == 'delete')	{
				$id = ORM::factory('page')->delete_all($arr_id);
				if($id)
					$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_del'));
			} elseif($sel_action == 'block') {
				foreach($arr_id as $arr => $id){
					$result = ORM::factory('page',$id);
			    	$result->page_active = 0;
			    	$id = $result->save();
		    	}
		    	if($id)
					$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_save'));
			} elseif($sel_action == 'active')
			{
				foreach($arr_id as $arr => $id){
					$result = ORM::factory('page',$id);
			    	$result->page_active = 1;
			    	$id = $result->save();
		    	}
		    	if($id)
					$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_save'));
			}			
		} else {
			$this->session->set_flash('warning_msg',Kohana::lang('errormsg_lang.msg_data_error'));
		}		

		url::redirect('admin_page/search');		
		die();
    }
    
    public function delete($id)
    {
    	$status = $this->db->delete('page',array('page_id' => $id));
    	if(count($status)>0)
		{
			$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_del'));
		}
    	url::redirect('admin_page/search');
    	die();
    }
    
    public function setstatus($id,$status)
    {
    	$status = $this->db->update('page',array('page_active' => $status),array('page_id' => $id));
    	if(count($status)>0)
		{
			$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_status_change'));
		}
    	url::redirect('admin_page/search'); 
    	die();
    }
}
?>